<?php
class Price
{
    var $DB_obj = null;
    var $meccano = null;
    var $error = false;

    function Price(&$DB)
    {
        $this->DB_obj = $DB;
        $this->meccano = &$GLOBALS['MECCANO'];
    }

    function Add($vars, $enterprise)
    {
        isDigit($enterprise);
        if($enterprise == false) return false;

        if(!is_array($vars)) $this->error = "There is no datas handled";
        if(!isset($vars['cat']) || $vars['cat'] == false) $this->error[] = "No Category selected";
        if(!isset($vars['title']) || $vars['title'] == false) $this->error[] = "No Title inputed";
        if(!isset($vars['price']) || $vars['price'] == false) $this->error[] = "No Price inputed";
        if(!isset($vars['units']) || $vars['units'] == false) $this->error[] = "No Units inputed";
        if(!isset($vars['currency']) || $vars['currency'] == false) $this->error[] = "No Currency inputed";
        if($this->error !== false) return false;

        $this->DB_obj->DB_QueryBuilder("INSERT INTO meccano_price (pid, cat, title, price, currency, units) VALUES (%i, %i, '%s', '%f', '%s', '%s')", array($enterprise, $vars['cat'], $vars['title'], $vars['price'], $vars['currency'], $vars['units']));
        $this->DB_obj->DB_Query();
    }


    function Get($enterprise = false, $category = false, $mode = 'a', $lang = 'ru')
    {
        $modes     = array('a','t','c','r','e');
        // OUTPUT ORDERS: a - as intput, t - by title, c - by category, r - random, e - by enterprise
        $sort      = ' ASC';
        $order     = '';
        $condition = '';

        if(strlen($mode) > 1)
        {
            if(substr($mode, 1, 1) == '-') $sort = ' DESC';
        }
        $mode = substr($mode, 0, 1);
        $mode = in_array($mode, $modes) ? $mode : 'a';

        switch ($mode)
        {
            case 'e':
                // Order by Enterprise
                $order = " ORDER BY BINARY ent.title" . $sort;
                break;

            case 't':
                // Order by Title
                $order = " ORDER BY BINARY pr.title" . $sort;
                break;

            case 'c':
                // Order by Category
                if(count($list) > 1 || $list === false)
                {
                    $order = ' GROUP BY BINARY trs.title' . $sort;
                }
                break;

            case 'r':
                // Random order
                $order = " ORDER BY RAND()";
                break;

            case 'a':
                // Order as inputed
            default:
                $order = " ORDER BY pr.id" . $sort;
        }

        isDigit($category);
        if($category == false) $category = $this->meccano->GetIdForNode('price');
        $categories = $this->meccano->ListOfID($category);
        $categories = implode(',',$categories);

        isDigit($enterprise);
        if($enterprise != false) $condition = " AND pr.pid = ".$enterprise;


        $this->DB_obj->sql = "SELECT pr.*, trs.title AS category, ent.title AS enterprise, ent.phones AS phones FROM meccano_price AS pr LEFT JOIN meccano_tree AS tr ON pr.cat = tr.id LEFT JOIN meccano_treetranslates AS trs ON tr.id = trs.id LEFT JOIN meccano_produce AS ent ON pr.pid = ent.id WHERE pr.cat IN (" . $categories . ") AND trs.lang = '" . $lang . "'" . $condition . $order;

        return $this->DB_obj->DB_Fetch();
    }


    function View($id, $lang='ru')
    {
        isDigit($id);
        if($id == false) return false;

        $this->DB_obj->sql = "SELECT pr.*, tr.title AS category FROM meccano_price AS pr LEFT JOIN meccano_tree AS tree ON tree.id = pr.cat LEFT JOIN meccano_treetranslates AS tr ON tree.id = tr.id WHERE tr.lang = '".$lang."' AND tree.module = 'price' AND pr.id = ". $id;

        return $this->DB_obj->DB_Fetch();
    }


    function Update($id, $vars, $enterprise)
    {
        isDigit($enterprise);
        if($enterprise == false) return  false;

        if(!is_array($vars)) $this->error = "There is no datas handled";
        if(!isset($vars['cat']) || $vars['cat'] == false) $this->error[] = "No Category selected";
        if(!isset($vars['title']) || $vars['title'] == false) $this->error[] = "No Title inputed";
        if(!isset($vars['price']) || $vars['price'] == false) $this->error[] = "No Price inputed";
        if(!isset($vars['units']) || $vars['units'] == false) $this->error[] = "No Units inputed";
        if(!isset($vars['currency']) || $vars['currency'] == false) $this->error[] = "No Currency inputed";
        if($this->error !== false) return false;

        $this->DB_obj->DB_QueryBuilder("UPDATE meccano_price SET cat=%i, title='%s', price='%f', currency='%s', units='%s' WHERE id = %i AND pid = %i", array($vars['cat'], $vars['title'], $vars['price'], $vars['currency'], $vars['units'], $id, $enterprise));

        return $this->DB_obj->DB_Query();
    }

    function Delete($id, $enterprise)
    {
        isDigit($enterprise);
        if($enterprise == false) return false;

        $del = array();

        if(is_array($id))
        {
            array_map("isDigit", $id);
            $del = array_values($id);
        } else
        {
            isDigit($id);
            if($id !== false) $del[] = $id;
        }
        unset($id);

        if(count($del) > 0)
        {
            $del = implode(',', $del);
            $this->DB_obj->sql = "DELETE FROM meccano_price WHERE id IN (".$del.") AND pid = ".$enterprise.";";

            return $this->DB_obj->DB_Query();
        }

        return false;
    }
}
?>